/**  
 * All rights Reserved, Designed By <a href="www.itttq.com" target="_blank">www.itttq.com</a>
 * @date:   2018年1月2日 下午4:42:56
 * @Copyright: 2018 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */  
package com.itttq.gshop.pbpm.provider;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.jdbc.SQL;

import com.itttq.gshop.domain.po.PayLogPo;
import com.itttq.gshop.pbpm.cmpt.mybatis.CustomerSQL;

/**   
 * @ClassName:  PayLogPoSqlProvider
 * @author: 迷蝶
 * @date:   2018年1月2日 下午4:42:56 
 * @Description: TODO
 *     
 * @Copyright: 2018 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */
public class PayLogSqlProvider {
	public static final String TABLE_NAME = "pay_log";

	/**
	 * 查找的SQL语句组装<br>
	 * 约定：分页两个参数为：start - 起始位置； size- 每页数量
	 * 
	 * @param payLog
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String findPayLogs(final PayLogPo payLog) {
		return new CustomerSQL() {{
			SELECT("content,create_time,id,log_type,pay_id,status ");
			FROM(TABLE_NAME);
			
			if (StringUtils.isNotBlank(payLog.getContent())) {
				WHERE("content=#{content}");
			}
			if (null != payLog.getLogType()) {
				WHERE("log_type=#{logType}");
			}
			if (null != payLog.getPayId()) {
				WHERE("pay_id=#{payId}");
			}
			if (null != payLog.getStatus()) {
				WHERE("status=#{status}");
			}
			if(StringUtils.isNotBlank(payLog.getSortField())) {
				ORDER_BY(payLog.getSortField() + (StringUtils.isNotBlank(payLog.getSortMethod()) ? "desc" : payLog.getSortField()));
			} else {
				ORDER_BY("id desc");
			}
			if(null != payLog.getStart() && null != payLog.getSize()) {
				LIMIT(payLog.getStart(), payLog.getSize());
			}
		}}.toString();
	}
	
	/**
	 * 查找记录总数的SQL语句组装
	 * 
	 * @param payLog
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String findPayLogCount(final PayLogPo payLog) {
		return new SQL() {{
			SELECT("count(*)");
			FROM(TABLE_NAME);
			if (StringUtils.isNotBlank(payLog.getContent())) {
				WHERE("content=#{content}");
			}
			if (null != payLog.getLogType()) {
				WHERE("log_type=#{logType}");
			}
			if (null != payLog.getPayId()) {
				WHERE("pay_id=#{payId}");
			}
			if (null != payLog.getStatus()) {
				WHERE("status=#{status}");
			}
		}}.toString();
	}
	
	/**
	 * 根据ID查找的SQL语句组装
	 * 
	 * @param id
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String findPayLogById(final Integer id) {
		return new SQL() {{
			SELECT("*");
			FROM(TABLE_NAME);
			WHERE("id = #{id}");
		}}.toString();
	}
	
	/**
	 * 增加的SQL语句组装
	 * 
	 * @param payLog
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String addPayLog(final PayLogPo payLog) {
		return new SQL() {{
			INSERT_INTO(TABLE_NAME);
			if (StringUtils.isNotBlank(payLog.getContent())) {
				VALUES("content","#{content}");
			}
			if (null != payLog.getLogType()) {
				VALUES("log_type","#{logType}");
			}
			if (null != payLog.getPayId()) {
				VALUES("pay_id","#{payId}");
			}
			if (null != payLog.getStatus()) {
				VALUES("status","#{status}");
			}
		}}.toString();
	}

	/**
	 * 修改的SQL语句组装
	 * 
	 * @param payLog
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String updatePayLog(final PayLogPo payLog) {
		return new SQL() {{
			UPDATE(TABLE_NAME);
			
			if (StringUtils.isNotBlank(payLog.getContent())) {
				SET("content=#{content}");
			}
			if (null != payLog.getLogType()) {
				SET("log_type=#{logType}");
			}
			if (null != payLog.getPayId()) {
				SET("pay_id=#{payId}");
			}
			if (null != payLog.getStatus()) {
				SET("status=#{status}");
			}
			WHERE("id = #{id}");
		}}.toString();
	}
	
	/**
	 * 根据ID删除的SQL语句组装
	 * 
	 * @param id
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String delPayLog(final Integer id) {
		return new SQL() {{
			DELETE_FROM(TABLE_NAME);
			WHERE("id = #{id}");
		}}.toString();
	}
}
